package com.gyms.view.bottomview;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.classic.okhttp.RequestBeans.HVAddProductOrderListRequestBean;
import com.classic.okhttp.beans.HVProductTypeItemBean;
import com.gyms.R;

import utils.DecimalUtils;
import utils.StringUtils;

import static java.lang.Integer.parseInt;

/**
 * 卡券预定框 yjl
 */
public class ScheduleDialog extends BaseBottomDialog {
    private LongClickButton buttonSub;
    private LongClickButton buttonAdd;
    private TextView numberTV, mTvTotalPrice, mName, mTvOnePrice;
    private Button mButton;
    private HVProductTypeItemBean mBean;
    private int limitNum;
    public ScheduleDialog(Context context, int animationStyles, HVProductTypeItemBean bean) {
        super(context, R.style.BottomViewTheme_Defalut);
        mBean = bean;
        if(bean.getMaxPayNum()==0) {
            limitNum = bean.getStockNum();
        }else{
            limitNum = Math.min(bean.getStockNum(), bean.getMaxPayNum());
        }
        animationStyle = animationStyles;
    }

    public ScheduleDialog(Context context, int animationStyles) {
        super(context, R.style.BottomViewTheme_Defalut);
        animationStyle = animationStyles;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_scheduled;
    }

    @Override
    protected void initView() {
        mButton = (Button) findViewById(R.id.btn_confirm);
        buttonSub = (LongClickButton) findViewById(R.id.tv_card_less);
        buttonAdd = (LongClickButton) findViewById(R.id.tv_card_add);
        numberTV = (TextView) findViewById(R.id.tv_card_number);
        mName = (TextView) findViewById(R.id.tv_card_name);
        mTvOnePrice = (TextView) findViewById(R.id.tv_card_prise);
        mTvTotalPrice = (TextView) findViewById(R.id.tv_total_price);

        if(limitNum>0){
            numberTV.setText("1");
        }

        hasEnable();
        hasEnableAdd();
        //连续减
        buttonSub.setLongClickRepeatListener(new LongClickButton.LongClickRepeatListener() {
            @Override
            public void repeatAction() {
                if (!hasEnable()) {
                    return;
                }
                numberTV.setText(String.valueOf(parseInt(numberTV.getText().toString()) - 1));
                Double prise = Integer.parseInt(numberTV.getText().toString()) * mBean.getDiscountPrice();
                setTextView(prise);
                hasEnableAdd();
            }
        }, 50);
        //连续加
        buttonAdd.setLongClickRepeatListener(new LongClickButton.LongClickRepeatListener() {
            @Override
            public void repeatAction() {
                if (!hasEnableAdd()) {
                    return;
                }
                numberTV.setText(String.valueOf(parseInt(numberTV.getText().toString()) + 1));
                Double prise = Integer.parseInt(numberTV.getText().toString()) * mBean.getDiscountPrice();
                setTextView(prise);
                hasEnable();
            }
        }, 50);

        //减1
        buttonSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numberTV.setText(String.valueOf(parseInt(numberTV.getText().toString()) - 1));
                Double prise = Integer.parseInt(numberTV.getText().toString()) * mBean.getDiscountPrice();
                setTextView(prise);
                hasEnable();
                hasEnableAdd();
            }
        });
        //加1
        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numberTV.setText(String.valueOf(parseInt(numberTV.getText().toString()) + 1));
                Double prise = Integer.parseInt(numberTV.getText().toString()) * mBean.getDiscountPrice();
                setTextView(prise);
                hasEnable();
                hasEnableAdd();
            }
        });

        if (!StringUtils.isNull(mBean)) {
            mName.setText(StringUtils.CS(mBean.getProductTypeName()));
//            mTvOnePrice.setText(DecimalUtils.doubleToIntOrD(mBean.getDiscountPrice()) + "元");
            mTvOnePrice.setText(DecimalUtils.setPriceStyle(getContext(),mBean.getDiscountPrice(),R.dimen.price_text_size_15, R.dimen.price_text_size_13,true));
            Double prise = Integer.parseInt(numberTV.getText().toString()) * mBean.getDiscountPrice();
            setTextView(prise);
        }

    }

    public void setOnClick(View.OnClickListener click) {
        mButton.setOnClickListener(click);
    }

    public HVAddProductOrderListRequestBean getRequestBean() {
        HVAddProductOrderListRequestBean bean = new HVAddProductOrderListRequestBean();
        bean.setCount(Integer.parseInt(numberTV.getText().toString()));
        Double prise = Integer.parseInt(numberTV.getText().toString()) * mBean.getDiscountPrice();
        bean.setTotalPrice(Double.parseDouble(DecimalUtils.doubleToIntOrD(prise)));
        bean.setProductTypeId(mBean.getProductTypeId());
        return bean;
    }

    /**
     * 判断是否为0
     */
    private boolean hasEnable() {
        if (0 >= parseInt(numberTV.getText().toString())) {
            buttonSub.setEnabled(false);
            mButton.setEnabled(false);
            return false;
        } else {
            mButton.setEnabled(true);
            buttonSub.setEnabled(true);
            return true;
        }
    }
    /**
     * 判断是否为超出
     */
    private boolean hasEnableAdd() {
        if(limitNum<=parseInt(numberTV.getText().toString())){
            buttonAdd.setEnabled(false);
            return false;
        }else {
            buttonAdd.setEnabled(true);
            return true;
        }
    }


    private void setTextView(Double prise) {
//        DecimalUtils.setPriceText(getContext(), DecimalUtils.doubleToIntOrD(prise), mTvTotalPrice, Constants.PRICE_ACTUAL_SIZE, Constants.YUAN_SIZE);
        mTvTotalPrice.setText(DecimalUtils.setPriceStyle(getContext(),prise,R.dimen.price_text_size_21, R.dimen.price_text_size_18,true));
    }
}
